/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.univalle.persistencia;

import co.edu.univalle.modelo.VentaInmueble;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Jhony
 */
@Stateless
public class VentasDAO implements IVentasDAO {
    
    @PersistenceContext
    private EntityManager entityManager;
    
    @Override
    public void registrar(VentaInmueble venta) {
        try
        {
            entityManager.persist(venta);
        }
        catch(RuntimeException e){
            throw e;
        }
    }

    @Override
    public void modificar(VentaInmueble venta) {
        try
        {
            entityManager.merge(venta);
        }
        catch(RuntimeException e){
            throw e;
        }
    }

    @Override
    public void eliminar(VentaInmueble venta) {
        try
        {
            entityManager.remove(venta);
        }
        catch(RuntimeException e){
            throw e;
        }
    }

    @Override
    public List<VentaInmueble> consultar() {
       try
        {
            String jpql = "select v from VentaInmueble v";
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        }
        catch(RuntimeException e){
            throw e;
        }
    }

    @Override
    public List<VentaInmueble> consultarPorCedulaComprador(Long cedula) {
        try{
            String jpql = "select v from VentaInmueble where v.cedula_comprador = "+cedula;
            Query query = entityManager.createQuery(jpql);
            return query.getResultList();
        }
        catch(RuntimeException e){
            throw e;
        }
    }

    // Add business logic below. (Right-click in editor and choose
    // "Insert Code > Add Business Method")
}
